<!--后台用户组-->
<div class="layuimini-container"> 
    <div class="layuimini-main">
        <fieldset class="layui-elem-field p20">
            <legend>说明</legend>
            <ol class="layui-field-box">
            <li>
                登录权限是指，在维护模式下，是否可以登录后台。
            </li>
            <li>
                修改用户组权限后，该用户组下的所有用户，如果有在线的，需要退出后重新登录才能生效
            </li>
                <li>
                关于用户组登录页面的说明：在用户登录时，index页面load哪个页面。
            </li>
        </ol>
        </fieldset>
        <div class="layui-form">
            <div class="layui-input-inline">
                <button class="layui-btn " data-page="page/group/admin_add.html"
                        data-click="common_add_click" >添加用户组</button>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary common-reload">
                    <i class="layui-icon-refresh-3 layui-btn-icon layui-icon"></i>重新加载</button>
            </div>
        </div>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs" lay-event="join">加权</a>
            <a class="layui-btn layui-btn-xs" lay-event="out">消权</a>
            <a class="layui-btn layui-btn-xs" lay-event="menu">菜单管理</a>
            <a class="layui-btn layui-btn-xs" lay-event="menu_ord">菜单排序</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script>
    tb = table.render({
            elem: '#currentTableId',
            url: '/admin/group/admin.htm',
            cols: [[
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'name', width: 120, title: '名称',},
                {field: 'login_url',  title: '登录页面',width:280},
                {field: 'notice',  title: '备注',templet:function (e){
                    return ( !e.notice || e.notice === '0') ? '--' : e.notice
                    }},
                {field: 'read_only_login', width: 100, title: '登录权限' , templet:function(e){
                    return e.read_only_login === 'y' ? '是' : '否';
                }},
                {title: '操作', width: 380, templet: '#currentTableBar',  align: "center"}
            ]],
            id:'group-table'
        });
        table.on('tool(currentTableFilter)', function (obj) {
            let title;
            const data = obj.data;
            $('#currentTableBar').attr('group-id',data.id).attr('curr-action',obj.event);
            if (obj.event === 'edit') {
                http.getHtml('page/group/admin_add.html',function(res){
                   openBox = layer.open({
                        type:1,
                        title:'修改用户组',
                        area:['600px','auto'],
                        content: res,
                        success:function(e){
                            $(e).find('legend').text('修改用户组')
                            layuimini.render($(e),data);
                            $(e).find('input[name="read_only_login"]').prop("checked",data.read_only_login == 'y' ? true : false);
                            form.render('checkbox','add-group');
                        }
                    })
                })
            }
            else if (obj.event === 'delete') {
                layer.confirm('你确定要删除吗？',function() {
                    http.get('/admin/group/admin_del.htm?id=' + data.auto_id).done(function (res) {
                        if (res.code == 200) {
                            setTimeout(function () {
                                obj.del()
                            }, 1200)
                        }
                        layer.msg(res.msg)
                    })
                })
            }
            else if ( obj.event === 'join' || obj.event === 'out'){
                let url, pri_table;
                title = '['+data.name+']';
                let page_title;
                if( obj.event === 'join' ){
                    url = '/admin/pri/has_nt_pri.htm'
                    title += '增加权限'
                    page_title = '增加权限';
                }
                else{
                    url = '/admin/pri/has_pri.htm'
                    title += '取消权限'
                    page_title = '取消权限'
                }
                http.getHtml('page/group/admin_pri_add.html',function(res){
                    window.openBox = layer.open({
                                type:1,
                                title:title,
                                area:['1050px','700px'],
                                content: res,
                                success:function(dom){
                                    $(dom).find('#pri-add-title').text(page_title)
                                       pri_table = table.render({
                                        elem: '#pri-add',
                                        url: url + '?id=' +data.id,
                                        cols: [[
                                            {type: "checkbox", width: 50, fixed: "left"},
                                            {field: 'id', width: 70, title: 'ID', sort: true},
                                            {field: 'url', width: 300, title: 'URL'},
                                            {field:'title', width:180,title:'类别'},
                                            {field: 'desc', title: '说明'},
                                            {title: '操作', width: 80, templet: '#pri-add-Bar-'+obj.event, fixed: "right", align: "center"}
                                        ]],
                                        limit: 8,
                                    });
                                    $(dom).find('.pri-search').off('click').on('click',function (e) {
                                            const key = $(this).parents('.layui-form').find('input[name="pri_key"]').val().trim();
                                            if(!! key ){
                                                pri_table.reload({
                                                    where:{
                                                        key:key
                                                    },
                                                    page:{
                                                        curr:1
                                                    }
                                                })
                                            }
                                    });
                                    $(dom).find('.pri-reload').off('click').on('click',function () {
                                            pri_table.reload({
                                                where:{key:''},page:{curr:1}
                                            })
                                    })
                                }
                    });
                })
            }
            else if (obj.event === 'menu'){
                title = '[' + data.name + ']';
                http.getHtml('page/group/admin_menu.html',function(res){
                    window.openBox = layer.open({
                        type:1,
                        title:'设置菜单'+title,
                        area:'450px',
                        content: res,
                        success:function(e){
                            http.get('/admin/group/admin_role_menu.htm?group_id='+data.id).done(function(ret){
                                    if(ret.code === 200 ){
                                        tree.render({
                                            elem:'#menu-body',
                                            data:ret.result.all_role_menu,
                                            showCheckbox:true,
                                            showLine:false,
                                            accordion:true,
                                            id:'menu-box'
                                        })
                                        tree.setChecked('menu-box',ret.result.group_role_menu_ids)
                                    }
                            })                          
                        }
                    })
                })
            }
            else if( obj.event === 'menu_ord' ){
                    const group_id = data.id;
                    http.get('/admin/role_menu.htm?group_id='+group_id + '&limit=300').done(function(res){
                        if( res.code === 0 ){
                            const data = res.data;
                            http.getHtml('page/group/admin_role_menu_sort.html',function(html){
                                window.openBox = layer.open({
                                    type:1,
                                    title:'修改排序',
                                    content:html,
                                    area:['650px','450px'],
                                    success:function(){
                                        window.treeTable.render({
                                            treeColIndex: 1,
                                            treeSpid: 0,
                                            treeIdName: 'id',
                                            treePidName: 'pid',
                                            elem: '#munu-table',
                                            width:630,
                                            treeDefaultClose:true,
                                            skin:'line',
                                            data:data,
                                            page: false,
                                            id:'menu-table',
                                            cols: [[
                                                {field:'map_id',title:'ID',width:60},
                                                {field: 'title', width: 200, title: '名称'},
                                                {field: 'href', title: '页面', templet:function(d){
                                                        return d.href === '0' ? '--' : d.href
                                                    }},
                                                {field:'ord',title:'排序',width: 120,edit: 'text'},
                                            ]],
                                        });
                                    }
                                })
                            })
                        }
                    })
            }
        });
    table.on('edit(munu-table)', function(obj){
        const ord = obj.value, map_id = obj.data.map_id;
            http.post('/admin/group/admin_role_menu_ord.htm',{id:map_id,ord:ord})
                .done(function(res){
                    layer.msg(res.msg)
                })
    });
    table.on('tool(pri-add)',function(obj){
            let id = obj.data.id, event = obj.event,
                group_id = $('#currentTableBar').attr('group-id'), url ='';
            layer.confirm('确认操作？',{icon: 3, title:'提示'},function ( index ) {
                if( event === 'join' ) {
                    url = '/admin/group/admin_add_pri.htm'
                } else if ( event === 'out') {
                    id = obj.data.map_id
                    url = '/admin/group/admin_del_pri.htm'
                }
                http.post(url,{ids:id,group_id:group_id}).done(function(res){
                    if(res.code === 200)
                    {
                        layer.close(openBox)
                        layer.close(index)
                    }
                    layer.msg(res.msg);
                })
            })
        })
    form.on('select(father)',function(data){
        const id = data.value;
        if( +id > 0 )
        {
            $('#role_menu_select').attr('data-role-menu-id',id)
            $(data.elem).parents('.layui-input-inline').next().remove()
            find_role_menu_by_pid(true,id,http,form,pri_tb)
        }

    })
        $(document).on('click','.data-btn-pri-all',function(){

            const type = $('#currentTableBar').attr('curr-action'),
                ids = layuimini.ids(table.checkStatus('pri-add').data, type === 'join' ? 'id' : 'map_id'),
                group_id = $('#currentTableBar').attr('group-id');
            if( ids.length < 1 )
            {
                layer.msg('请选择至少一个权限！');
                return ;
            }
            const info = type == 'out' ? '删除' : '加入';
            const url = type == 'out' ? '/admin/group/admin_del_pri.htm' : '/admin/group/admin_add_pri.htm';
            layer.confirm('你确定全部'+info+'吗？',{icon:3},function(index){
                http.post(url,{ids:ids,group_id:group_id}).done(function(res){
                    if(res.code == 200)  {
                        layer.close(index);
                        layer.close(openBox)
                    }
                    layer.msg(res.msg);
                })
            })            
        })
        $(document).off('click','.data-btn-menu-save').on('click','.data-btn-menu-save',function(){
            const checkData = tree.getChecked('menu-box'),
                auto_ids = new Array(),
                group_id = $('#currentTableBar').attr('group-id');
            const auto_ids_fun = function (checkData) {
                checkData.map(function (v) {
                    auto_ids.push(v.id);
                    if (v.children) {
                        auto_ids_fun(v.children);
                    }
                })
            };
            auto_ids_fun(checkData)
            if( !group_id ){
                layer.msg('没有获取到组ID');
                return ;
            }
            http.post('/admin/group/admin_role_menu_save.htm',{ids:auto_ids,group_id:group_id}).done(function(res){
                if(res.code === 200) {
                    layer.close(openBox);
                }
                layer.msg(res.msg)
            })
        })
</script>